package com.snopy.concurrency.demo_base;

import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author
 * @description:semaphore信号量  控制并发线程数
 * @date 2020/9/2712:11
 */
@Slf4j(topic = "test3")
public class Test3 {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Semaphore semaphore = new Semaphore(10);
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i <100 ; i++) {
            Map<String,Object> map = new HashMap<>(1);
            map.put("k"+i,"v"+i);
            list.add(map);
        }

        for (Map<String,Object> map : list) {

                     executorService.submit(()->{
                         try {
                             semaphore.acquire();
                             System.out.println("task");
                             Thread.sleep(500);
                         } catch (InterruptedException e) {
                             e.printStackTrace();
                         }finally {
                             semaphore.release();
                         }
                     });
        }
    }
}
